﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using TaoSH.AppCode;
using TaoSH.Data;

namespace TaoSH.View
{
    public partial class MangerUser : BasePage
    {
        public string strAction
        {
            get
            {
                return Request.QueryString["Action"].ToString();
            }
        }

        public string strBanchCode
        {
            get
            {
                return Request.QueryString["PID"] ?? "-1";
            }
        }

        public string strUserName
        {
            get
            {
                return Request.QueryString["UserName"] ?? "-1";
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitialData();
            }
        }

        protected void btAddUser_Click(object sender, EventArgs e)
        {
            if (ChenckData())
            {
                if (strAction == "add")
                {
                    JH_Auth_User User = new JH_Auth_UserB().GetEntities(d => d.UserName == tbLogonName.Text.ToString().Trim()).SingleOrDefault();
                    if (User != null)
                    {
                        AlertMSG("2", "已经存在相同用户！");

                    }
                    else
                    {
                        JH_Auth_User NewUser = new JH_Auth_User();
                        NewUser.UserName = tbLogonName.Text.ToString().Trim();
                        NewUser.UserRealName = tbRealName.Text.ToString().Trim();
                        NewUser.zhiwu = tbZhiWu.Text.ToString();
                        NewUser.UserPass = "123";
                        NewUser.IsUse = ckIsUser.Checked ? "Y" : "N";
                        NewUser.tx = "TX10";
                        NewUser.remark = "-1";
                        NewUser.BranchCode = int.Parse(ddlBranch.SelectedValue);
                        NewUser.STATUS = "";
                        new JH_Auth_UserB().Insert(NewUser);
                        AlertMSG("2", "添加用户成功！");
                    }

                }
                else
                {
                    JH_Auth_User UserUP = new JH_Auth_UserB().GetEntities(d => d.UserName == strUserName).SingleOrDefault();
                    UserUP.UserName = tbLogonName.Text.ToString().Trim();
                    UserUP.UserRealName = tbRealName.Text.ToString().Trim();
                    UserUP.BranchCode = int.Parse(ddlBranch.SelectedValue);
                    UserUP.zhiwu = tbZhiWu.Text.ToString();
                    UserUP.IsUse = ckIsUser.Checked ? "Y" : "N";
                    new JH_Auth_UserB().Update(UserUP);
                    AlertMSG("2", "修改用户成功！");
                }
            }
            else
            {
                return;
            }
        }

        /// <summary>
        /// 服务器端验证
        /// </summary>
        /// <returns></returns>
        public bool ChenckData()
        {
            bool boolcheck = true;
            if (tbLogonName.Text.ToString() == "")
            {


                AlertMSG("1", "登录名不能为空！");
                boolcheck = false;
            }
            if (new JH_Auth_UserB().GetEntities(d => d.UserName == strUserName).Count() >= 1 && strAction == "add")
            {
                AlertMSG("1", "已存在相同用户名！");

                boolcheck = false;
            }

            return boolcheck;
        }

        public void InitialData()
        {
            new CommonHelp.BindData().BindBranch(ddlBranch);
            if (strAction == "add")
            {
                ddlBranch.SelectedValue = strBanchCode;
            }
            else
            {
                JH_Auth_User User = new JH_Auth_UserB().GetEntities(d => d.UserName == strUserName).SingleOrDefault();
                tbLogonName.Text = User.UserName;
                tbRealName.Text = User.UserRealName;

                ckIsUser.Checked = User.IsUse == "Y" ? true : false;
                ddlBranch.SelectedValue = User.BranchCode.ToString();
            }
        }
    }
}